﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevExpress.XtraEditors;

namespace QLnhasach
{
    public partial class formDangnhap : XtraForm
    {

        private KetNoiCSDL ketnoi = new KetNoiCSDL();
        private NguoiDung nguoidung = new NguoiDung();
        private SqlDataReader reader;

        public Boolean Tuychon_act = false;
        public formDangnhap()
        {
            InitializeComponent();
            this.Height = 280;
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle("McSkin");
        }

        private void btn_Tuychon_Click(object sender, EventArgs e)
        {
            if (!Tuychon_act)
            {
                this.Height = 464;                
                Tuychon_act = true;
            }
            else
            {
                this.Height = 280;
                Tuychon_act = false;
            }
        }

        private void rg_Taikhoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rg_Taikhoan.SelectedIndex.Equals(0))
            {
                textEdit_TaikhoanSQL.Enabled = false;
                textEdit_MatkhauSQL.Enabled = false;              
            }
            else
            {
                textEdit_TaikhoanSQL.Enabled = true;
                textEdit_MatkhauSQL.Enabled = true;
            }
        }

        private void btn_Dangnhap_Click(object sender, EventArgs e)
        {
            String t1 = textEdit_Taikhoan.Text;
            String t2 = textEdit_Matkhau.Text;
            formChinh fmChinh;
            
            int kiemtra = KiemTra(t1, t2);
            if (kiemtra == 1)
            {
                labelControl6.Text = "Đăng Nhập thành công - Admin - " + nguoidung.tenDangNhap;
                layThongTin(nguoidung, reader);
                this.Hide();
                fmChinh = new formChinh(nguoidung);                
                fmChinh.Show();
                this.Close();
            }
            else if (kiemtra == 2)
            {
                labelControl6.Text = "Đăng Nhập thành công - User -" + nguoidung.tenDangNhap;
                layThongTin(nguoidung, reader);
                fmChinh = new formChinh(nguoidung);
                fmChinh.Show();
                this.Close();
            }
            else
                labelControl6.Text = "Tên Đăng Nhập Hoặc Mật Khẩu bị sai!";

            //this.Close();
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void textEdit_TaikhoanSQL_MouseDown(object sender, MouseEventArgs e)
        {
            if (textEdit_TaikhoanSQL.Text.Equals("Tài khoản SQL"))
                textEdit_TaikhoanSQL.Text = null;
        }

        private void textEdit_MatkhauSQL_MouseDown(object sender, MouseEventArgs e)
        {
            if (textEdit_MatkhauSQL.Text.Equals("Mật khẩu"))
                textEdit_MatkhauSQL.Text = null;
        }

        private int KiemTra(String tenDangNhap, String MatKhau)
        {
            String t1 = "SELECT * FROM NHANVIEN WHERE TAIKHOAN = '" + tenDangNhap + "'and MATKHAU = '" + MatKhau + "' and ADMIN = 'admin'";
            String t2 = "SELECT * FROM NHANVIEN WHERE TAIKHOAN = '" + tenDangNhap + "'and MATKHAU = '" + MatKhau + "'";
            reader = ketnoi.thihanhDataReader(t1);            
            if (reader.Read())//neu nguoi dung la admin tra ve 1
                return 1;
            else
            {
                reader = ketnoi.thihanhDataReader(t2);
                if (reader.Read())//neu nguoi dung la user tra ve 2
                    return 2;
            }
            return 0;//neu nguoi dung khong hop le tra ve 0
        }

        private void layThongTin(NguoiDung user, SqlDataReader reader)
        {
            int j = 0;
            user.MaNhanVien = reader.GetString(j++);
            user.TenNhanVien = reader.GetString(j++);
            user.NgaySinh = reader.GetDateTime(j++);
            user.SoDienThoai = reader.GetString(j++);
            user.DiaChi = reader.GetString(j++);
            user.NgayVaoLam = reader.GetDateTime(j++);
            user.tenDangNhap = reader.GetString(j++);
            user.MatKhau = reader.GetString(j++);
            user.ChucVu = reader.GetString(j++);
            user.PhanQuyen = reader.GetString(j++);
            //nhanvien.dateTimeNgaySinh = reader.GetDateTime(3);    
            //nhanvien.dateTimeNgaySinh = Convert.ToDateTime(reader.GetDateTime(3));
        }
    }
}